<template>
  <el-dialog :title="dialogTitle" :visible.sync="visible" width="50%" @close="handleClose">
    <el-form :model="form" label-width="160px" ref="orderFormRef" :rules="rules">
      <!-- 客户经理名称 -->
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="客户经理名称" prop="managerName">
            <el-input v-model="form.managerName"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="客户经理联系电话" prop="managerPhone">
            <el-input v-model="form.managerPhone"></el-input>
          </el-form-item>
        </el-col>
      </el-row>

      <!-- 部门和职位 -->
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="部门" prop="department">
            <el-input v-model="form.department"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="职位" prop="position">
            <el-input v-model="form.position"></el-input>
          </el-form-item>
        </el-col>
      </el-row>

      <!-- 邮箱 -->
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="邮箱" prop="email">
            <el-input v-model="form.email"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="handleCancel">取 消</el-button>
      <el-button type="primary" @click="handleSubmit">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  name: "ManagerInfoSyncForm",
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    dialogTitle: {
      type: String,
      default: ""
    },
    formData: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    return {
      form: { ...this.formData },
      rules: {
        managerName: [{ required: true, message: '请输入客户经理名称', trigger: 'blur' }],
        managerPhone: [{ required: true, message: '请输入客户经理联系电话', trigger: 'blur' }],
        department: [{ required: true, message: '请输入部门', trigger: 'blur' }],
        position: [{ required: true, message: '请输入职位', trigger: 'blur' }],
        email: [{ required: true, message: '请输入邮箱', trigger: 'blur' }]
      }
    };
  },
  methods: {
    handleSubmit() {
      this.$refs.orderFormRef.validate((valid) => {
        if (valid) {
          this.$emit('submit', { ...this.form });
          this.$message.success('操作成功');
          this.handleCancel();
        } else {
          console.log('表单验证失败!');
          return false;
        }
      });
    },
    handleCancel() {
      this.$emit('update:visible', false);
    },
    handleClose() {
      this.$emit('update:visible', false);
    },
  },
  watch: {
    formData: {
      handler(newVal) {
        this.form = { ...newVal };
      },
      deep: true
    }
  }
};
</script>

<style scoped lang="scss">
/* 样式可以根据需要自定义 */
</style>
